<template>
  <el-dialog
    title="编辑用户信息"
    :visible.sync="dialogVisible"
    width="80%"
    :before-close="handleClose">
    <el-form :model="user" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户id">
        <el-input v-model="user.id" readonly></el-input>
      </el-form-item>
      <el-form-item label="用户名称" prop="name">
        <el-input v-model="user.name"></el-input>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="user.age"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="user.email"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm()">立即编辑</el-button>
        <el-button  @click="resetForm(user)">重置</el-button>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer"></span>
  </el-dialog>
</template>
<script>
  export default {
    name: 'edit',
    props:{
      //是否编辑
      dialogVisible: {
        type:Boolean,
        default:false
      },
      //用户对象
      user: {},
      rules: {
        name: [
          {required: true, message: '请输入用户名称', trigger: 'blur'}
        ],
        age: [
          {required: true, message: '请输入用户年龄', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入用户邮箱', trigger: 'blur'}
        ]
      }
    },
    methods: {
      //提交编辑
      submitForm() {
        var $this = this;
        $this.axios.post('http://localhost:8082/edit', $this.user)
          .then(function (response) {
            $this.dialogVisible = false;
            $this.user = {};
            $this.query($this.current, $this.pageSize, $this.search.name);
          })
      },
      //重置
      resetForm(formName){
        if (this.$refs[formName]!=undefined) {
          this.$refs[formName].resetFields();
        }
      },
      //关闭弹窗
      handleClose(done) {
        var $this = this;
        $this.dialogVisible = false;
      }
    }
  }
</script>

